﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Useful.GUI.HierarchyDataGridView
{
    public class DataGridViewColumn2LevelHelperHeaderCell : DataGridViewColumnHeaderCell
    {
        private readonly DataGridViewColumn2LevelHeaderCell _BaseCell;
        private bool isChecked;
        private Rectangle cbRect = new Rectangle(20, 0, 20, 20);

        public bool Checked
        {
            get
            {
                return isChecked;
            }
        }

        public DataGridViewColumn2LevelHelperHeaderCell(DataGridViewColumn2LevelHeaderCell baseCell)
        {
            this._BaseCell = baseCell;
        }

        private DataGridViewColumn2LevelHeaderCell FindCH()
        {
            return _BaseCell;
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("DataGridViewColumn2LevelHelperHeaderCell.Paint - rowIndex = {0}, ColumnIndex = {1}, colName = {2}, cellBounds = {3}", rowIndex, ColumnIndex, DataGridView.Columns[ColumnIndex].Name, cellBounds));
            System.Diagnostics.Debug.WriteLine(String.Format("DataGridViewColumn2LevelHelperHeaderCell.Paint(BaseCell) - rowIndex = {0}, ColumnIndex = {1}, colName = {2}, cellBounds = {3}", _BaseCell.RowIndex, _BaseCell.ColumnIndex, DataGridView.Columns[_BaseCell.ColumnIndex].Name, _BaseCell.ContentBounds));

            DataGridViewColumn2LevelHeaderCell ch = null;
            int x = cellBounds.X;

            for (int i = ColumnIndex - 1; i >= 0 && ch == null; i--)
            {
                if (DataGridView.Columns[i].HeaderCell == _BaseCell)
                {
                    ch = _BaseCell;
                }

                x -= DataGridView.Columns[i].Width;
            }

            if (ch != null)
            {
                int with = ch.GetWidth();
                int height = ch.GetHeight(cellBounds.Height);

                Rectangle rect1 = new Rectangle(x, cellBounds.Y, with, height);

                ch.DrawCell(graphics, rect1);

                Rectangle rect = new Rectangle(cellBounds.X, cellBounds.Y + height, cellBounds.Width, cellBounds.Height - height);

                base.Paint(graphics, clipBounds, rect, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

                PaintBorder2(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
            }
            else
                throw new Exception("ch == null");
        }

        public bool IsMouseInCell(Point p)
        {
            Rectangle rect1 = DataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);

            DataGridViewColumn2LevelHeaderCell ch = FindCH();
            int height = ch.GetHeight(rect1.Height);

            Rectangle rect2 = new Rectangle(new Point(rect1.X + cbRect.X, rect1.Y + height + cbRect.Y), cbRect.Size);

            return !rect2.Contains(p);
        }

        public void ProcessCheck()
        {
            isChecked = !isChecked;
        }

        protected void PaintBorder2(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("DataGridViewColumn2LevelHelperHeaderCell.PaintBorder: RowIndex = {0}, ColumnIndex = {1}", this.RowIndex, this.ColumnIndex));

            base.PaintBorder(graphics, clipBounds, bounds, cellStyle, advancedBorderStyle);

            int w = DataGridView.Columns[ColumnIndex].DividerWidth;

            if (w != 0)
            {
                using (Pen p = new Pen(DataGridView.GridColor, w))
                {
                    graphics.DrawLine(p, bounds.Right - w, bounds.Y, bounds.Right - w, bounds.Bottom);
                }
            }
        }
    }
}
